<template>
  <!-- el-header 容器 每個el ui的組件的可以类名使用-->
  <el-container>
    <!-- 头部 el-header-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>燎原之海</span>
      </div>

      <div>
        <el-popover
          placement="bottom"
          width="400"
          trigger="hover"
          content="hello"
          style="margin-right:20px"
        >
          <el-card>
            <el-row type="flex" justify="center">
              <el-col :span="6">
                <div>{{this.$store.state.userInfo.user.name}}</div>
              </el-col>
            </el-row>
            <div v-if="this.$store.state.userInfo.user.typeClassify == 2">
              <el-row>
                <el-col :span="6">
                  <div>等级: {{this.$store.state.userInfo.levelNum}}</div>
                </el-col>
                <el-col :span="6" :offset="12">
                  <div>{{this.$store.state.userInfo.user.levelValue}}/{{this.$store.state.userInfo.endExperienceValue}}</div>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <div>
                    <el-progress
                      :percentage="parseInt((this.$store.state.userInfo.user.levelValue/this.$store.state.userInfo.endExperienceValue)*100)"
                    ></el-progress>
                  </div>
                </el-col>
              </el-row>
            </div>

            <!-- 失约数 和 完成约定数 -->
            <div v-if="this.$store.state.userInfo.user.typeClassify == 1">
              <el-row>
                <el-col :span="6" :offset="3">
                  <el-tooltip class="item" effect="dark" content="已完成的奖励" placement="left-end">
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-aixin" />
                    </svg>
                  </el-tooltip>
                  : {{this.$store.state.userInfo.finishCommitmentCount}}
                </el-col>
                <el-col :span="6" :offset="8">
                  <el-tooltip class="item" effect="dark" content="超时的奖励" placement="left-end">
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-xin-liehen" />
                    </svg>
                  </el-tooltip>
                  : {{this.$store.state.userInfo.timeoutCount}}
                </el-col>
              </el-row>
            </div>

            <el-row>
              <el-col :span="6" v-if="this.$store.state.userInfo.user.typeClassify == 2">
                <div>
                  <i class="el-icon-s-finance" style="font-size:20px" />
                  {{this.$store.state.userInfo.user.money}}
                </div>
              </el-col>
              <el-col :span="6" :offset="12">
                <div>
                  <el-badge>
                    <i class="el-icon-message" style="font-size:20px" @click="skipEmail"></i>
                  </el-badge>
                </div>
              </el-col>
            </el-row>
          </el-card>
          <i class="el-icon-s-custom" slot="reference" style="font-size:50px"></i>
        </el-popover>

        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏  宽度绑定是否折叠的bool-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="togleCollapse">|||</div>
        <!-- el-menu是菜单的包裹性容器 router开启以index属性为路由的跳转-->
        <el-menu
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409FFF"
        >
          <!-- :unique-opened="true"->只允许展开一个菜单 -->
          <!-- :collapse-transition="false" -> 关闭动画 -->
          <!-- router -> 导航开启路由模式 -->
          <!-- 一级菜单  的index 如果相同那么点开一个的时候全部都会被点开 一级菜单如果不需要渲染组件的话只需要保持唯一性即可-->
          <!-- :index代表的是属性绑定到 index -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- i指定图标 根据菜单的id去自定义的数组里拿到图标的位置-->
              <i :class="iconObj[item.id]"></i>
              <!-- 指定文本 -->
              <span>{{item.name}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.child"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 导航开启路由模式：
              将index值作为导航路由-->
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { getMenuList } from "@/utils/SupervisorApi";
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 默认不折叠
      isCollapse: false,
      // 被激活导航地址
      activePath: "",
      userInfo: {},
    };
  },
  //组件创建的钩子函数
  created() {
    // console.log(this.$store)
    this.getMenuList();
    this.$store.commit("getUserInfo");
    this.activePath = window.sessionStorage.getItem("activePath");
  },

  methods: {
    skipEmail() {
      this.$router.push("/supervisoremail");
    },

    logout() {
      // 清空token
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取请求菜单
    async getMenuList() {
      const { data: res } = await getMenuList();
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data.menus;
      console.log(this.menuList);
    },
    // 菜单的折叠与展开
    togleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存连接的激活地址
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
    },
  },
};
</script>

<style lang="less" scoped>
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 40px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  // 鼠标放上去变成小手
  cursor: pointer;
}
</style>
